package com.gientecher.redisdemo.controller;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
public class RedisConfigController {

    //基于provider项目启动两个实例，使用实例1往redis内生成 key:myconfig  value:this is my config
    // 使用实例2redis获取key为myconfig的值，并返回到页面上。
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/setConfig")
    public String setValue(){
        String key = "myconfig";
        String value = "this is my config";

        stringRedisTemplate.opsForValue().set(key, value, 10, TimeUnit.SECONDS);
        return "set succ";
    }

    @GetMapping("/getConfig")
    public String getValue(){
        String key = "myconfig";
        String str = stringRedisTemplate.opsForValue().get(key);

        return str;
    }
}